package cn.net.withub.dataCollector.web.collector;

import cn.edu.hfut.dmic.webcollector.model.CrawlDatums;
import cn.edu.hfut.dmic.webcollector.model.Page;
import cn.edu.hfut.dmic.webcollector.plugin.berkeley.BreadthCrawler;
import cn.net.withub.dataCollector.common.editor.DateUtil;
import cn.net.withub.dataCollector.common.model.TCollectorConfig;
import cn.net.withub.dataCollector.common.model.TCollectorContent;
import cn.net.withub.dataCollector.common.model.TCollectorData;
import cn.net.withub.dataCollector.common.model.TCollectorElement;
import cn.net.withub.dataCollector.common.model.TCollectorLog;
import cn.net.withub.dataCollector.common.utils.CreateNewKey;
import cn.net.withub.dataCollector.web.service.CollectorService;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.apache.log4j.Logger;
import org.springframework.web.context.WebApplicationContext;

/**
 * 新浪网国内新闻
 */
public class CrawlerBase extends BreadthCrawler {

    //@Resource
    protected CollectorService collectorService;

    //@Resource
    protected WebApplicationContext webAppliction;

    protected TCollectorConfig tCollectorConfig;

    protected Logger log = Logger.getLogger(this.getClass());
    protected SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

    public void CollectorUtils(CollectorService collectorService){
        this.collectorService = collectorService;
    }

    public CrawlerBase(String crawlPath, boolean autoParse, WebApplicationContext wac, TCollectorConfig config) {
        super(crawlPath, autoParse);
        webAppliction = wac;
        if(webAppliction !=null) collectorService = (CollectorService) webAppliction.getBean("collectorService");
        if(config != null) tCollectorConfig = config;
    }

    public void visit(Page page, CrawlDatums crawlDatums) {

    }
    public void saveData(String id,String title,String fbsj,String xxly,String wjgs, String url,String bjsh,String xxfb,
                         String wjlj,String xxzz) throws Exception {
        TCollectorData tCollectorData = new TCollectorData();
        tCollectorData.setId(id);
        tCollectorData.setConfigId(tCollectorConfig.getId());
        tCollectorData.setTitle(title);
        tCollectorData.setFbsj(sdf.parse(fbsj));
        tCollectorData.setXxly(xxly);
        tCollectorData.setWjgs(wjgs);
        tCollectorData.setUrl(url);
        tCollectorData.setSystime(new Date());
        tCollectorData.setFydm(tCollectorConfig.getFydm());
        tCollectorData.setFymc(tCollectorConfig.getFymc());
        collectorService.save(tCollectorData);
    }
    public void saveContent(String dataId,String content,String text) throws Exception {
        TCollectorContent tCollectorContent = new TCollectorContent();
        tCollectorContent.setId(new CreateNewKey().createId());
        tCollectorContent.setContent(content);
        tCollectorContent.setSystime(new Date());
        tCollectorContent.setDataId(dataId);
        tCollectorContent.setText(text);
        collectorService.save(tCollectorContent);
    }
    public void saveElement(String dataId,String imageName,String src,String serverFilePath) throws Exception {
        TCollectorElement tCollectorElement = new TCollectorElement();
        tCollectorElement.setId(new CreateNewKey().createId());
        tCollectorElement.setDataId(dataId);
        tCollectorElement.setTitle(imageName);
        tCollectorElement.setUrl(src);
        tCollectorElement.setWjlj(serverFilePath);
        tCollectorElement.setSystime(new Date());
        collectorService.save(tCollectorElement);
    }
    public void saveMaxFbsj(String fbsj) throws Exception {
        TCollectorConfig config = (TCollectorConfig)collectorService.load(TCollectorConfig.class," id='"+tCollectorConfig.getId()+"'");
        if(config.getFbsj()==null || DateUtil.compareDate(fbsj,sdf.format(config.getFbsj()))>0){
            config.setFbsj(sdf.parse(fbsj));
            collectorService.save(config);
        }
    }

    public void saveLog(String title,String fbsj,String firstUrl,String tqUrl,int zt){
        try {
            log.info("savelog........."+fbsj);
            TCollectorLog tCollectorLog = new TCollectorLog();
            tCollectorLog.setId(new CreateNewKey().createId());
            tCollectorLog.setConfigId(tCollectorConfig.getId());
            tCollectorLog.setIp(tCollectorConfig.getIp());
            tCollectorLog.setModuleName(tCollectorConfig.getModuleName());
            tCollectorLog.setFydm(tCollectorConfig.getFydm());
            tCollectorLog.setFymc(tCollectorConfig.getFymc());
            tCollectorLog.setTitle(title);
            try {
                tCollectorLog.setFbsj(sdf.parse(fbsj));
            }catch (Exception e) {
                e.printStackTrace();
            }
            tCollectorLog.setZt(zt);
            tCollectorLog.setFirstUrl(firstUrl);
            tCollectorLog.setTqUrl(tqUrl);
            tCollectorLog.setSysTime(new Date());

            collectorService.save(tCollectorLog);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    String getStr(Object obj){
        return obj == null || "".equals(obj.toString()) ? "" : obj.toString();
    }
    Integer getInt(Object obj){
        return obj == null || "".equals(obj.toString()) ? 0 : Integer.parseInt(obj.toString());
    }

    /**
     * 对爬虫url中包含空格和中文的url进行编码处理
     * @param url
     * @return
     * @throws UnsupportedEncodingException
     */
    public String fmtUrl(String url) throws UnsupportedEncodingException {
        if("".equals(url)) return "";
        //对中文部分编码
        Pattern p = Pattern.compile("([\u4e00-\u9fa5]+)");
        Matcher m = p.matcher( url );
        //url = url.replaceAll("\\+","%20");//空格转为%20
        url = url.replaceAll("\\s+","%20");//空格转为%20
        String mv = null;
        //List<String> list=new ArrayList<String>();
        if(m.find()){//此处默认只有一处中文,后面有需求在改
            mv = m.group(0);
            mv = URLEncoder.encode(mv, "utf-8");
            //list.add(getURLEncoderString(mv));
            //url=url.replace("[\\u4e00-\\u9fa5]", mv);
            url=url.replaceAll("[\u4e00-\u9fa5]+", mv);
            /*while (m.find()) {
                mv = m.group(0);
                mv = URLEncoder.encode(mv, "utf-8");
                //list.add(getURLEncoderString(mv));
                url=url.replaceAll("[\\u4e00-\\u9fa5]", mv);
            }*/
        }
        return url;
    }

    /**
     * 判断是否包含中文
     * @param str
     * @param str2  需要替换的中文字符
     * @return
     */
    public static boolean isContainChinese(String str,String str2) throws UnsupportedEncodingException {
        Pattern p = Pattern.compile("[\u4e00-\u9fa5]+");
        Matcher m = p.matcher(str);
        if (m.find()) {
            str2 = m.group(0);
            str2 = URLEncoder.encode(str2, "utf-8");
            return true;
        }
        return false;
    }

}
